package org.jground.rx.ui.zk.viewControl.extra;

import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import org.jground.codetable.Codetable;
import org.jground.codetable.DefaultCodetable;
import org.jground.codetable.DefaultCodetableElement;
import org.jground.rx.RXCodetableFactory;
import org.jground.rx.RXSystem;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.viewControl.UIAttributeControlBase;
import org.jground.rx.ui.zk.viewControl.UIAttributeViewControlFactory;
import org.jground.util.ArrayUtils;
import org.jground.util.L;
import org.jground.zk.zulX.ComboboxX;
import org.zkoss.zk.ui.Component;

public class UIAttributeControlVoting extends UIAttributeControlBase {

    static {
        UIAttributeViewControlFactory.registerControlFactory(UIAttributeViewVoting.WIDGET_HINT,
                new UIAttributeControlVoting());
    }

    private static Log logger = LogFactory.getLog(UIAttributeControlVoting.class);

    private Codetable ct;
    private ComboboxX codeTableUI;
    private VotingList votingList = new VotingList();

    public IUIAttributeView<Component,UIFrame> createNew(UIFrame uiFrame, RXView moControl) {
        return new UIAttributeControlVoting(uiFrame, moControl);
    }

    public UIAttributeControlVoting() {
        super();
    }

    private UIAttributeControlVoting(UIFrame uiFrame, RXView moControl) {
        super(uiFrame, moControl);
        L.debug(logger, "codetable name", UIAttributeViewVoting.VOTING_CODETABLE_NAME);
        try {
            ct = RXSystem.getInstance().getCtService().getCodetable(UIAttributeViewVoting.VOTING_CODETABLE_NAME, true);
        } catch (Exception e) {
            logger.error("Did not find codetable for 'voting'!");
            ct = new DefaultCodetable(UIAttributeViewVoting.VOTING_CODETABLE_NAME, e.toString(),
                    new DefaultCodetableElement("--", e.toString()));
        }
        codeTableUI = new ComboboxX(ct);
        codeTableUI.setReadonly(true);
        // codeTableUI.setModel(codeTableModel = new CodeTableListModel(ct));
        add(codeTableUI);

    }

    public void set(String... values) {
        clear();
        String userName = RXSystem.getRXPrincipalMethods().getUserName();
        if (ArrayUtils.isNotEmpty(values) && values.length % 2 == 0) {
            votingList.initByDBValues(values);
            String code = votingList.getVote(userName);
            if (code != null) {
                codeTableUI.setSelected(code);
            }
        }
    }

    public String[] get() {
        String code = codeTableUI.getSelected();
        if (code != null) {
            String userName = RXSystem.getRXPrincipalMethods().getUserName();
            votingList.setVote(userName, code);
        }
        return votingList.getDBValues();
    }

    public void clear() {
        if (codeTableUI.getItemCount() > 0) {
            codeTableUI.setSelectedIndex(0);
        }
        votingList.clear();
    }

}
